package edu.westga.cs3202.sokoban;

import java.io.*;
import java.util.*;

/**
 * Reads in the file representing the board and handles movement logic.
 * 
 * @author Lewis Williams
 * @version 2011.2.17
 */
public class GameBoard {
	private char [][] theBoard;
	private char [][] originalCloneBoard;
	private int playerRow;
	private int playerColumn;
	private int boardRows;
	private int boardColumns;
	private String boardSource = "pacman.txt";

    public GameBoard() {
    	setBoardDimensions(boardSource);
    	theBoard = new char[boardRows][boardColumns];
    	originalCloneBoard = new char[boardRows][boardColumns];
    	populateBoard(boardSource);
    }
    
    public char getCharacter(int row, int col) {
    	return theBoard[row][col];
    }    
    
    /**
     * The move method moves the player (and boulders if necessary) 
     * by the amounts specified in the parameters
     * 
     * @param rowShiftByOne
     * 			the amount to change the row index to move one square
     * @param rowShiftByTwo
     * 			the amount to change the row index to move two squares
     * @param columnShiftByOne
     * 			the amount to change the column index to move one square
     * @param columnShiftByTwo
     * 			the amount to change the column index to move two squares
     */
    private void move(int rowShiftByOne, int rowShiftByTwo, int columnShiftByOne, int columnShiftByTwo){
    	String nextTwoTiles = "";
    	//Checks to make sure the the row or column the player is attempting to enter 
    	//is not the first or last (the entire board must be surrounded by walls)
    	if(playerColumn + columnShiftByTwo == -1 || playerRow + rowShiftByTwo == -1 || playerColumn + columnShiftByTwo == boardColumns || playerRow + rowShiftByTwo == boardRows){
    		nextTwoTiles = "bb";
    	}
    	else{
    		nextTwoTiles = nextTwoTiles + String.valueOf(theBoard[playerRow + rowShiftByOne][playerColumn + columnShiftByOne]) + String.valueOf(theBoard[playerRow + rowShiftByTwo][playerColumn + columnShiftByTwo]);
    	}
    	if(theBoard[playerRow + rowShiftByOne][playerColumn + columnShiftByOne] != '|' && 
    	   theBoard[playerRow + rowShiftByOne][playerColumn + columnShiftByOne] != '_' &&
    	   theBoard[playerRow + rowShiftByOne][playerColumn + columnShiftByOne] != '<' &&
    	   theBoard[playerRow + rowShiftByOne][playerColumn + columnShiftByOne] != '>' &&
    	   theBoard[playerRow + rowShiftByOne][playerColumn + columnShiftByOne] != '^' &&
    	   theBoard[playerRow + rowShiftByOne][playerColumn + columnShiftByOne] != 'v' &&
    	   
    	   !nextTwoTiles.equalsIgnoreCase("bb") && !nextTwoTiles.equalsIgnoreCase("b5") &&
    	   !nextTwoTiles.equalsIgnoreCase("b6") && !nextTwoTiles.equalsIgnoreCase("b7") &&
    	   !nextTwoTiles.equalsIgnoreCase("b8") && !nextTwoTiles.equalsIgnoreCase("b9") &&
    	   !nextTwoTiles.equalsIgnoreCase("b|") && !nextTwoTiles.equalsIgnoreCase("b_") &&
    	   !nextTwoTiles.equalsIgnoreCase("b<") && !nextTwoTiles.equalsIgnoreCase("b>") &&
    	   !nextTwoTiles.equalsIgnoreCase("b^") && !nextTwoTiles.equalsIgnoreCase("bv") &&
    	   !nextTwoTiles.equalsIgnoreCase("b1") && !nextTwoTiles.equalsIgnoreCase("b2") &&
    	   !nextTwoTiles.equalsIgnoreCase("b3") && !nextTwoTiles.equalsIgnoreCase("b4") &&
    	   !nextTwoTiles.equalsIgnoreCase("b5")){
    		if(nextTwoTiles.charAt(0) == ' ' || nextTwoTiles.charAt(0) == 'g'){
    			theBoard[playerRow][playerColumn] = originalCloneBoard[playerRow][playerColumn];
    			playerColumn = playerColumn + columnShiftByOne;
    			playerRow = playerRow + rowShiftByOne;
    			theBoard[playerRow][playerColumn] = 'p';
    		}
    		else if(nextTwoTiles.charAt(0) == 'b'){
    			theBoard[playerRow][playerColumn] = originalCloneBoard[playerRow][playerColumn];
    			theBoard[playerRow + rowShiftByTwo][playerColumn + columnShiftByTwo] = 'b';
    			playerColumn = playerColumn + columnShiftByOne;
    			playerRow = playerRow + rowShiftByOne;   	
    			theBoard[playerRow][playerColumn] = 'p';		
    		}
    	}    	
    }
    
    
    public void moveLeft(){
    	move(0,0,-1,-2);	
    }
    
    public void moveRight() {
    	move(0,0,1,2);
	}

	public void moveUp() {
		move(-1,-2,0,0);
	}

	public void moveDown() {
		move(1,2,0,0);
	}
    
    public int getBoardRows(){return boardRows;}
    public int getBoardColumns(){return boardColumns;}
    
    /**
     * Reads in the board file and maps it to theBoard and the originalCloneBoard
     * @param filename
     * 			The name of the file being read in.
     */
    private void populateBoard(String filename){
    	try{
    		//Creates the file
    		File boardFile = new File(filename);
    		//Creates the scanner to read the file.
    		Scanner scan = new Scanner(boardFile);
    		int row = -1;
    		//Populates theBoard by reading in and parsing the lines of the file.
    		while(scan.hasNext()){
    			row ++;
    			//Obtains the next line
    			String line = scan.nextLine();
    			//Loops through the line
    			for(int column = 0; column < line.length(); column++){    				
    				theBoard[row][column] = line.charAt(column);
    				//Sets the playerRow and playerColumn when 'p' is found
    				if(line.charAt(column) == 'p'){
    					playerRow = row;
    					playerColumn = column;
    				}
    				//Populates a clone of the board replacing boulders with blank spaces to help in movement redrawing
    				originalCloneBoard[row][column] = line.charAt(column);
    				if(line.charAt(column) == 'b'){
    					originalCloneBoard[row][column] = ' ';
    				}
    			}
    		}
    		//Replaces where the player is found with a blank space in the clone board to help in movement redrawing.
    		originalCloneBoard[playerRow][playerColumn] = ' ';
    	}
    	//Catches FileNotFoundException if the board file cannot be found.
    	catch(FileNotFoundException fnfe){
    		System.out.println("The board file cannot be found.");
    	}
    }
    
    public void setBoardDimensions(String filename){
    	try{
    		//Creates the file
    		File boardFile = new File(filename);
    		//Creates the scanner to read the file.
    		Scanner scan = new Scanner(boardFile);
    		int rowCount = 0;
    		int columnCount = 0;
    		//Populates theBoard by reading in and parsing the lines of the file.
    		while(scan.hasNext()){
    			rowCount ++;
    			//Obtains the next line
    			String line = scan.nextLine();
    			if(line.length() > columnCount){
    				columnCount = line.length();
    			}
    		}
    		boardRows = rowCount;
    		boardColumns = columnCount;
    	}
    	//Catches FileNotFoundException if the board file cannot be found.
    	catch(FileNotFoundException fnfe){
    		System.out.println("The board file cannot be found.");
    	}
    }
    
    public char[][] getBoard(){
    	char [][] copyBoard = new char[boardRows][boardColumns];
    	for(int row = 0; row < boardRows; row++){
    		for(int column = 0; column < boardColumns; column++){
    			copyBoard[row][column] = theBoard[row][column];
    		}
    	}
    	return copyBoard;
    }

	
}